package com.eastidea.live.util;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	private static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May",
			"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	private static final String[] WEEK_DAYS = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
		"Saturday" };
	
	public static Date StringToDate(String format, String dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
	   try {
	       Date date = dateFormat.parse(dateStr.trim());
	       return date;
	   } catch (Exception e) {
	       e.printStackTrace();
	   }
	   return null;
	}
	public static String DateToString(String format, Date dt) {
		if (dt == null)
			return "";
		SimpleDateFormat sdf  = new  SimpleDateFormat(format);
		return sdf.format(dt);
	}
	
	public static String ToDateStr(Date dt) {
		if (dt == null)
			return "";
		SimpleDateFormat sdf  = new  SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(dt);
	}
	
	public static String ToTimeStr(Date dt) {
		if (dt == null)
			return "";
		SimpleDateFormat sdf  = new  SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(dt);
	}

	public static String ToEnglishDayWithMon(Date dt) {
		if (dt == null)
			return "";
		DateFormatSymbols sym = new DateFormatSymbols();
		sym.setMonths(MONTHS);
		SimpleDateFormat f = new SimpleDateFormat("dd MMM, yyyy", sym);

		return getWeekDay(dt) + ", " + f.format(dt);
	}
	
	public static String ToEnglishDay(Date dt) {
		if (dt == null)
			return "";
		DateFormatSymbols sym = new DateFormatSymbols();
		sym.setMonths(MONTHS);
		SimpleDateFormat f = new SimpleDateFormat("dd MMM, yyyy", sym);

		return f.format(dt);
	}
	
	public static Date GetTodayMorning() {
    	Calendar c = Calendar.getInstance();  
        c.setTime(new Date());  
        c.set(Calendar.HOUR_OF_DAY, 0);  
        c.set(Calendar.MINUTE, 0);  
        c.set(Calendar.SECOND, 0);
    	return c.getTime();
	}
	
	public static Date combineDateAndTime(String dt, String tm) {
    	if (StringUtil.IsEmpty(dt)) {
    		return null;
    	}

    	if (StringUtil.IsEmpty(tm)){
    		tm = "08:00";
    	}
    	String fullStr2 = dt + " " + tm;    	
    	return DateTimeUtil.StringToDate("yyyy-MM-dd HH:mm", fullStr2);
    }
	
	private static String getWeekDay(Date date) {
		  Calendar cal = Calendar.getInstance();
		  cal.setTime(date);
		  int idx = cal.get(Calendar.DAY_OF_WEEK) - 1;
		  return WEEK_DAYS[idx];
	}
	

}
